/*
 * Copyright (C) 2013-2015 Zhang Rui <bbcallen@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "IJKMediaControl.h"
#import <IJKMediaFramework/IJKMediaPlayback.h>
//#import <IJKMediaFramework/IJKMediaFramework.h>

@implementation IJKMediaControl
{
    BOOL _isMediaSliderBeingDragged;
    
    long long inBytes; //网络下行吞吐量
    int avgSpeedMinute;//每分钟平均下载速度
    int instantSpeed;//瞬时下载速度

}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initControlViewWithFrame:frame];
    }
    return self;
}

- (void)initControlViewWithFrame:(CGRect)frame
{
    _overlayPanel = [[UIView alloc] init];
    [self addSubview:_overlayPanel];
    
    [_overlayPanel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.bounds.size);
        make.edges.left.right.bottom.top.equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];

}

- (void)showNoFadeWith:(int)authenState
{
    self.overlayPanel.alpha = 1;
    [self cancelDelayedHide];
    [self refreshMediaControlWith:authenState];
}

- (void)showAndFadeWith:(int)authenState
{
    [self showNoFadeWith:authenState];
    if (authenState != 0) {
        [self performSelector:@selector(hide) withObject:nil afterDelay:5];
    }
}

- (void)hide
{
    self.overlayPanel.alpha = 0;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MediaControlHidden" object:nil];
    [self cancelDelayedHide];
}

- (void)cancelDelayedHide
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hide) object:nil];
}

- (void)beginDragMediaSlider
{
    _isMediaSliderBeingDragged = YES;
}

- (void)endDragMediaSlider
{
    _isMediaSliderBeingDragged = NO;
}

- (void)continueDragMediaSliderWith:(int)authenState
{
    [self refreshMediaControlWith:authenState];
}

- (void)refreshMediaControlWith:(int)authenState
{
    // duration
    NSTimeInterval duration = self.delegatePlayer.duration;
    NSInteger intDuration = duration;
    if (intDuration > 0) {
        self.mediaProgressSlider.maximumValue = duration;
        self.totalDurationLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", (int)(intDuration / 3600), (int)((intDuration % 3600) / 60), (int)(intDuration % 60)];
    } else {
        self.totalDurationLabel.text = @"--:--:--";
        self.mediaProgressSlider.maximumValue = 1.0f;
    }

    
    // position
    NSTimeInterval position;
    if (_isMediaSliderBeingDragged) {
        position = self.mediaProgressSlider.value;

    } else {
        position = self.delegatePlayer.currentPlaybackTime;
    }
    NSInteger intPosition = position;
    if (intDuration > 0) {
        self.mediaProgressSlider.value = position;
    } else {
        self.mediaProgressSlider.value = 0.0f;
    }
    self.currentTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", (int)(intPosition / 3600), (int)((intPosition % 3600) / 60), (int)(intPosition % 60)];
    
    __block long long inBytesNow;

  //  inBytesNow = [Tools getInterfaceBytes];
    instantSpeed = (int)((inBytesNow - inBytes)/1000);
    inBytes = inBytesNow;

//    long long speed = [Tools getInterfaceBytes];
    
//    NSLog(@"speed is %d",instantSpeed);

    self.mediaProgressSlider.trackValue = [[NSString stringWithFormat:@"%.2f",self.delegatePlayer.playableDuration / self.delegatePlayer.duration] floatValue];
    
    // status
    BOOL isPlaying = [self.delegatePlayer isPlaying];
    self.playButton.selected = isPlaying;

//    NSLog(@"intPosition is %ld, authenState is %d",(long)intPosition, authenState);


//    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshMediaControlWith:) object:[NSNumber numberWithInt:authenState]];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshMediaControlWith:) object:nil];
    if (self.overlayPanel.alpha == 1) {
//        [self performSelector:@selector(refreshMediaControlWith:) withObject:[NSNumber numberWithInt:authenState] afterDelay:0.5];
        [self performSelector:@selector(refreshMediaControlWith:) withObject:nil afterDelay:0.5];
    }
    
    if (authenState == 0) {
        if (intPosition > 10) {
//            [self.delegatePlayer shutdownAndShowTheView];
        }
    }

}



@end
